/*
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 */
function onDeleteProduct(url, productId, resp) {
	var isOK = -1;
	
	if(!confirm('Are you sure you want to delete the product with Id:' + productId)) {
		return false;
	}
	
	new Ajax.Request(url,  {
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=delproduct&id=' + productId,
	  onSuccess: function(transport) {
			var response = transport.responseText || "";
			var pos=response.indexOf('<!');
			content=response.substring(0,pos);
		    	//alert(content);

			obj = content.evalJSON();

    		if(obj == null) {
    			$(resp).innerHTML = 'Something wrong! Reponsed string is: ' + response;
    		}
    		
    		isOK = obj.code;
		
    		$(resp).update(obj.message);
		}
	});

	$(resp).style.display = 'inherit';
	Fat.fade_element(resp, null, 1500, null, null);

	if(isOK == 0) {
		rowID = 'product-' + productId;
		Fat.fade_element(rowID, null, 1000, "#f22", null);
		setTimeout("$(rowID).remove()", 1000);
	}				
}

function onDeletePhotos(url, photosId,namephoto, resp) {
	var isOK = -1;
	
	if(!confirm('Are you sure you want to delete the Photo with Id:' + photosId)) {
		return false;
	}
	
	new Ajax.Request(url,  {
	  method: 'post',
	  asynchronous: false,
	  parameters:'act=delphoto&id=' + photosId+'&name='+namephoto,
	  onSuccess: function(transport) {
			var response = transport.responseText || "";
			var pos=response.indexOf('<!');
			content=response.substring(0,pos);
		    	//alert(content);

			obj = content.evalJSON();

    		if(obj == null) {
    			$(resp).innerHTML = 'Something wrong! Reponsed string is: ' + response;
    		}
    		
    		isOK = obj.code;
		
    		$(resp).update(obj.message);
		}
	});

	$(resp).style.display = 'inherit';
	Fat.fade_element(resp, null, 1500, null, null);

	if(isOK == 0) {
		rowID = 'photos_one_' + photosId;
		Fat.fade_element(rowID, null, 1000, "#f22", null);
		setTimeout("$(rowID).remove()", 1000);
	}				
}


function onAddProductPhotos(frId,inputId) {
	var totalId = document.getElementById(inputId);
	var fraddphotos = document.getElementById(frId);
	var divtag = fraddphotos.getElementsByTagName("tr");
	var newvalue = parseInt(totalId.value) +1 ;
	var tr_head = document.createElement("tr");
		fraddphotos.appendChild(tr_head);				
	var th_head = document.createElement("th");
		th_head.width = "33%";
		th_head.scope = "row";
		th_head.valign = "top";
		th_head.innerHTML = 'Image '+newvalue+':';
		tr_head.appendChild(th_head);	
	var td_head = document.createElement("td");
		td_head.width = "67%";
		tr_head.appendChild(td_head);		
	var file_head = document.createElement("input");
		file_head.type = "file";
		file_head.name = "product_image_"+newvalue;
		td_head.appendChild(file_head);	
		totalId.value = newvalue;
}

function toggleSub(url,id,parentid,expand,closed,groupid,catid,checkgroupid,sumgroupid){
		var child= document.getElementById(id);
		var parent= document.getElementById(parentid);
			checkinput = child.getElementsByTagName('div');
		var checkgroup= document.getElementById(checkgroupid);	
		var sumgroup= document.getElementById(sumgroupid);	
		if(checkinput.length>0){
			
		}else{
			new Ajax.Request(url,  {
			  method: 'post',
			  asynchronous: false,
			  parameters:'act=getproduct&id=' + groupid+'&catid='+catid,
			  onSuccess: function(transport) {
					var response = transport.responseText || "";
					//alert(response);
					obj = response.evalJSON();
					var p =0;
					var ie = document.all;
					for(t=0;t<obj.length;t++){
						var divtag = document.createElement("div");
							divtag.className = "assignleaf";
							child.appendChild(divtag);
						var checkbox = document.createElement("input");
							checkbox.type = "checkbox";
							checkbox.value = obj[t].product_id;
							checkbox.name = "p_"+obj[t].product_id;
							if(obj[t].active_product!=""  && checkgroup.checked != false){
								checkbox.checked = true;
								p++;
							}
							checkbox.onclick = function(e){
								checkedGroup('group_'+groupid,this,id);
							};
							divtag.appendChild(checkbox);
						var spanname = document.createElement("span");
							spanname.innerHTML = obj[t].product_name;
							spanname.id = obj[t].product_thumbnail;
							spanname.className = 'point';
							spanname.onmouseover = function(e){
								var obj = ie ? event.srcElement : e.target;
								showtrail(obj.id,obj.innerHTML,120,100);
							};
							spanname.onmouseout = function(e){
								hidetrail();
							};
							divtag.appendChild(spanname);
					}
					sumgroup.value = t;
					if(p==0 && checkgroup.checked==true){
						formcheck = child.getElementsByTagName('input');
						for (var i =0; i < formcheck.length; i++){
								formcheck[i].checked = true;
						}
					}
				}
			});
		}
		if(child.style.display != 'block'){
			parent.src = expand;
			child.style.display = 'block';
		}else{
			parent.src = closed;	
			child.style.display = 'none';
		}
		
		
}